package top.kaoshanji.p2ch17a;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 代码清单-p2ch17a-527页a：Path对象
 * author kaoshanji
 * ^_^
 * create time 2025/3/11 11:19
 */
public class PathInfo {

    public static void main(String[] args) {
        System.out.println(System.getProperty("os.name"));

        // 其实并没有这个目录
        info(Paths.get("C:", "path", "to", "nowhere", "NoFile.txt"));

        Path p = Paths.get("PathInfo.java");
        info(p);

        Path ap = p.toAbsolutePath();
        info(ap);
        info(ap.getParent());

        try {
            info(p.toRealPath());
        } catch(IOException e) {
            System.out.println(e);
        }
        URI u = p.toUri();
        System.out.println("URI:\n" + u);
        Path puri = Paths.get(u);
        System.out.println(Files.exists(puri));
        File f = ap.toFile(); // 不要被骗了

    }

    static void info(Path p) {
        show("toString:\n ", p);
        show("Exists: ", Files.exists(p));
        show("RegularFile: ", Files.isRegularFile(p));
        show("Directory: ", Files.isDirectory(p));
        show("Absolute: ", p.isAbsolute());
        show("FileName: ", p.getFileName());
        show("Parent: ", p.getParent());
        show("Root: ", p.getRoot());
        System.out.println("******************");
    }

    static void show(String id, Object p) {
        System.out.println(id + p);
    }

}
